package com.gohome.gohomechat.ui.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.SpannableString;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.MessageBody;
import com.easemob.chat.VoiceMessageBody;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.model.entify.Recorder;
import com.gohome.gohomechat.utils.FaceConversionUtil;
import com.gohome.gohomechat.utils.TextUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/3/4.
 */
public class ChatListViewAdapter extends BaseAdapter {
    private List<EMMessage> datas;
    private static final int contact_right = 0;
    private static final int contact_left = 1;
    private onMessageClickListener listener;
    private int clickPosition;
    private Context context;
    private LayoutInflater inflater;
    private int mMinItemWith;// 设置对话框的最大宽度和最小宽度
    private int mMaxItemWith;
    private List<Recorder> recorders;

    public ChatListViewAdapter(Context context) {
        this.context = context;
        recorders = new ArrayList<>();
        datas = new ArrayList<>();
    }

    public void setOnMessageLongClickListener(onMessageClickListener listener) {
        this.listener = listener;
    }

    public ChatListViewAdapter(Context context, List<Recorder> dataList) {
        inflater = LayoutInflater.from(context);
        // 获取系统宽度
        WindowManager wManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wManager.getDefaultDisplay().getMetrics(outMetrics);
        mMaxItemWith = (int) (outMetrics.widthPixels * 0.7f);
        mMinItemWith = (int) (outMetrics.widthPixels * 0.15f);
        datas = new ArrayList<>();
    }

    public void addVoiceData(EMMessage emMessage) {
        if (datas != null) {
            datas.add(emMessage);
            notifyDataSetChanged();
        } else {
            Log.e("$$$$", "kongl");
        }
    }

    public void addData(List<EMMessage> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }
    /**
     * 刷新页面
     */
    public void refresh() {
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        EMMessage message = datas.get(position);
        if (message.getFrom().equals(message.getUserName()))
            return contact_left;
        return contact_right;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (datas != null && datas.size() > 0) {
            RightViewHolder rightViewHolder = null;
            LeftViewHolder leftViewHolder = null;
            switch (getItemViewType(position)) {
                case contact_right:

                    if (convertView == null) {
                        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_listview_item_right, parent, false);
                        rightViewHolder = new RightViewHolder(convertView);
                        convertView.setTag(rightViewHolder);
                    } else {
                        rightViewHolder = (RightViewHolder) convertView.getTag();
                    }
                    break;
                case contact_left:

                    if (convertView == null) {
                        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_listview_item_left, parent, false);
                        leftViewHolder = new LeftViewHolder(convertView);
                        convertView.setTag(leftViewHolder);
                    } else {
                        leftViewHolder = (LeftViewHolder) convertView.getTag();
                    }
                    break;
            }
            final EMMessage message = datas.get(position);
            final MessageBody messageBody = message.getBody();
            switch (getItemViewType(position)) {
                case contact_right:
                    if (message.getType() == EMMessage.Type.TXT) {
                        String text = TextUtil.splitTxt(messageBody.toString());
                        SpannableString spannableString = FaceConversionUtil.getInstace().getExpressionString(parent.getContext(), text);
                        rightViewHolder.chat_item_me_text.setVisibility(View.VISIBLE);
                        rightViewHolder.chat_item_me_img.setVisibility(View.GONE);
                        rightViewHolder.chat_item_me_text.setText(spannableString);
                    } else if (message.getType() == EMMessage.Type.IMAGE) {
                        ImageMessageBody imageMessageBody = (ImageMessageBody) messageBody;
                        if (imageMessageBody.getLocalUrl() != null) {
                            String remotePath = imageMessageBody.getRemoteUrl();
                            //   String thumbnaiUrl = imageMessageBody.getThumbnailUrl();
                            rightViewHolder.chat_item_me_img.setVisibility(View.VISIBLE);
                            rightViewHolder.chat_item_me_text.setVisibility(View.GONE);
                            Picasso.with(context).load(remotePath).into(rightViewHolder.chat_item_me_img);
                            Log.i("wcl", imageMessageBody.getLocalUrl() + "图片地址");
                            Log.i("wcl", imageMessageBody.getRemoteUrl() + "图片地址getRemoteUrl");
                            Log.i("wcl", imageMessageBody.getThumbnailUrl() + "图片地址getThumbnailUrl");
                        }
                    } else if (message.getType() == EMMessage.Type.VOICE) {

                        VoiceMessageBody voiceBody = (VoiceMessageBody)message.getBody();
                        int len = voiceBody.getLength();
                        if(len>0){
                            rightViewHolder.chat_item_me_voice_len.setText(voiceBody.getLength() + "\"");
                            rightViewHolder.chat_item_me_voice_len.setVisibility(View.VISIBLE);
                        }else{
                            rightViewHolder.chat_item_me_voice_len.setVisibility(View.GONE);
                        }
                        voiceBody.getLocalUrl();
                        rightViewHolder.chat_item_me_text.setVisibility(View.GONE);
                        rightViewHolder.chat_item_me_img.setVisibility(View.GONE);
                        rightViewHolder.chat_item_me_voice.setVisibility(View.VISIBLE);
                        ((AnimationDrawable)rightViewHolder.chat_item_me_voice.getDrawable()).start();
                    }


                    rightViewHolder.chat_item_me_text.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            listener.onMessageLongClick(v, message.getMsgId(), messageBody.toString());
                            return false;
                        }
                    });
                    break;
                case contact_left:
                    if (message.getType() == EMMessage.Type.TXT) {
                        String text = TextUtil.splitTxt(messageBody.toString());
                        SpannableString spannableString = FaceConversionUtil.getInstace().getExpressionString(parent.getContext(), text);
                        leftViewHolder.chat_item_friend_text.setVisibility(View.VISIBLE);

                        leftViewHolder.chat_item_img.setVisibility(View.GONE);

                        leftViewHolder.chat_item_friend_text.setText(spannableString);
                    } else if (message.getType() == EMMessage.Type.IMAGE) {
                        ImageMessageBody imageMessageBody = (ImageMessageBody) messageBody;
                        if (imageMessageBody.getLocalUrl() != null) {
                            String remotePath = imageMessageBody.getRemoteUrl();
                            leftViewHolder.chat_item_img.setVisibility(View.VISIBLE);

                            leftViewHolder.chat_item_friend_text.setVisibility(View.GONE);
                            Picasso.with(context).load(remotePath).into(leftViewHolder.chat_item_img);
                        }
                    } else if (message.getType() == EMMessage.Type.VOICE) {
                        Log.e("ChatListViewAdapter", "加载Ui");
                        Log.e("ChatListViewAdapter", "加载Ui");
                        leftViewHolder.chat_item_img.setVisibility(View.GONE);
                        leftViewHolder.chat_item_friend_text.setVisibility(View.GONE);

                        leftViewHolder.lengthFriend.setVisibility(View.VISIBLE);

                    }

                    leftViewHolder.chat_item_friend_text.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            listener.onMessageLongClick(v, message.getMsgId(), messageBody.toString());
                            return false;

                        }
                    });
                    break;

            }
            return convertView;
        }
        return null;
    }


    class RightViewHolder {
        private TextView chat_item_me_text, chat_item_me_voice_len;
        private View lengthMe;
        private ImageView chat_item_me_imageView, chat_item_me_img,chat_item_me_voice;

        public RightViewHolder(View view) {
            chat_item_me_text = (TextView) view.findViewById(R.id.chat_item_me_text);
            chat_item_me_imageView = (ImageView) view.findViewById(R.id.chat_item_me);
            chat_item_me_img = (ImageView) view.findViewById(R.id.chat_item_me_img);
            chat_item_me_voice= (ImageView) view.findViewById(R.id.chat_item_me_voice);
            chat_item_me_voice_len= (TextView) view.findViewById(R.id.chat_item_me_voice_len);

        }
    }

    class LeftViewHolder {
        private TextView chat_item_friend_text, chat_item_friend_voiceSeconds;
        private View lengthFriend;
        private ImageView chat_item_friend_imageView, chat_item_img;

        public LeftViewHolder(View view) {
            chat_item_friend_text = (TextView) view.findViewById(R.id.chat_item_text);
            chat_item_friend_imageView = (ImageView) view.findViewById(R.id.chat_item_friend);
            chat_item_img = (ImageView) view.findViewById(R.id.chat_item_img);

        }
    }

    public interface onMessageClickListener {
        void onMessageLongClick(View view, String msgId, String message);
    }
}
